#!/bin/bash
# Perf C2C private options
# - event
# - mode
# - ldlat
# Perf record common options
# - delay
# - duration

. $LKP_SRC/lib/env.sh
. $LKP_SRC/lib/common.sh

set_perf_path "/lkp/benchmarks/perf/perf"

is_virt && exit 0

: "${duration:=10}"

if [ -z "$delay" ]; then
	if [ -n "$runtime" ]; then
		delay=$((runtime / 2))
	else
		delay=100
	fi
fi

[ -n "$event" ] && opt_event="-e $event"
[ -n "$mode" ] && opt_mode="-${mode}"
[ -n "$ldlat" ] && opt_ldlat="-l $ldlat"

. $LKP_SRC/lib/wait.sh
setup_wait

perf_c2c_data=$TMP/perf_c2c.data

$perf -v || die 'perf command failed'
$perf c2c record -ag $opt_event $opt_mode $opt_eldlat -o \
	$perf_c2c_data -D $((delay * 1000)) -- \
	$WAIT_POST_TEST_CMD --timeout $((delay + duration))

cat > "$TMP_RESULT_ROOT/post-run.perf-c2c" <<EOF
[ -s "$perf_c2c_data" ] && {
	$perf c2c report --stdio --call-graph none -i $perf_c2c_data  > $RESULT_ROOT/perf-c2c-profile.log
	$perf c2c report -NN -i $perf_c2c_data --stats | gzip >	$RESULT_ROOT/perf-c2c.gz && {
		cp $perf_c2c_data $RESULT_ROOT
		cp /proc/kallsyms $RESULT_ROOT
	}
}
EOF
